package com.soikc.kuaidiE.wx.controller;

import com.soikc.kuaidiE.bean.Courier;
import com.soikc.kuaidiE.bean.Message;
import com.soikc.kuaidiE.bean.User;
import com.soikc.kuaidiE.bean.UserEax;
import com.soikc.kuaidiE.mvc.ResponseBody;
import com.soikc.kuaidiE.service.CourierService;
import com.soikc.kuaidiE.service.UserService;
import com.soikc.kuaidiE.util.RandomUtil;
import com.soikc.kuaidiE.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

public class UserController {
    private UserUtil userUtil = new UserUtil();

    @ResponseBody("/api/wx/login")
    public String login(HttpServletRequest req, HttpServletResponse res) {
        String phone = req.getParameter("phone");
        String userCode = req.getParameter("code");
        HttpSession session = req.getSession();
        String sysCode = userUtil.getLoginCode(session, phone);
        Message message;
        if (sysCode == null) {
            message = new Message(-1, "请先获取验证码");
        } else if (sysCode.equals(userCode)) {
            message = new Message(0, "登入成功");
            Courier courier = CourierService.queryPhone(phone);
            User user = UserService.queryPhone(phone);

            if (courier == null && user == null) {//用户不存在则创建新的用户
                user = new User();
                user.setPhone(phone);
                user.setPassword("88888888");
                user.setRegister_time(new Date());
                user.setLogin_time(new Date());
                user.setNickname("新用户");
                UserService.add(user);
                user =UserService.queryPhone(phone);
                if (user != null) {
                    message.setResult("注册成功！");
                }else {
                    message.setResult("注册失败！");
                }
            }
            if (courier != null) {
                message.setStatus(1);
                courier.setLogin_time(new Date());
                userUtil.setWxUser(session, courier);
            } else {
                user.setLogin_time(new Date());
                userUtil.setWxUser(session, user);
            }
        } else {
            message = new Message(-2, "验证码不正确");
        }
        userUtil.setLoginCode(session, phone, "");
        return message.toString();
    }

    @ResponseBody("/api/wx/loginSms")
    public String loginSms(HttpServletRequest req, HttpServletResponse res) {
        String phone = req.getParameter("phone");
        String code = (RandomUtil.get() + "").substring(0, 4);
        userUtil.setLoginCode(req.getSession(), phone, code);
        Message message = new Message(0, "验证码获取成功：\n" + code);
        return message.toString();
    }

    @ResponseBody("/api/wx/logout")
    public String logout(HttpServletRequest req, HttpServletResponse res) {
        req.getSession().invalidate();
        Message message = new Message(0, "退出完成");
        return message.toString();
    }

    @ResponseBody("/api/wx/userInfo")
    public String userInfo(HttpServletRequest req, HttpServletResponse res) {
        Object wxUser = userUtil.getWxUser(req.getSession());
        Message message;
        if (wxUser == null) {
            message = new Message(-1, "你还未登入");
        } else {
            UserEax userEax = (UserEax) wxUser;
            if (wxUser instanceof User) {
                message = new Message(0, "获取成功", userEax.toViveObj());
            } else if (wxUser instanceof Courier) {
                message = new Message(1, "获取成功", userEax.toViveObj());
            } else {
                message = new Message(-3, "未知错误");
            }
        }
        return message.toString();
    }

    @ResponseBody("/api/wx/updateUser")
    public String updateUser(HttpServletRequest req, HttpServletResponse res) {
        HttpSession session = req.getSession();
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String mail = req.getParameter("mail");
        String password = req.getParameter("password");
        String msg = req.getParameter("msg");
        String code = userUtil.getLoginCode(session, phone);
        Message message = new Message(-2, "未知错误");
        if (code == null) {
            message = new Message(-1, "请先获取验证码");
        } else if (!code.equals(msg)) {
            message = new Message(-1, "验证码错误");
        } else {
            UserEax wxUser = userUtil.getWxUser(session);
            if (wxUser != null) {
                wxUser.setPhone(phone);
                wxUser.setPassword(password);
                wxUser.setMail(mail);
                wxUser.setNickname(name);
            } else {
                message = new Message(-1, "你还没有登入");
            }
            if (wxUser instanceof User) {
                boolean b = UserService.update(wxUser.getId(), (User) wxUser);
                message = new Message(b ? 0 : -1, b ? "修改成功" : "修改失败");
            } else if (wxUser instanceof Courier) {
                boolean b = CourierService.update(wxUser.getId(), (Courier) wxUser);
                message = new Message(b ? 0 : -1, b ? "修改成功" : "修改失败");
            }
        }
        userUtil.setLoginCode(session, phone, null);
        return message.toString();
    }
}
